package com.example.occlusiondemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.widget.TextView;

import com.eqgis.eqtool.tmp.Utils;
import com.example.occlusiondemo.PathConfig;
import com.example.occlusiondemo.R;
import com.google.ar.core.exceptions.NotYetAvailableException;
import com.supermap.ar.areffect.AREffectView;
import com.supermap.ar.areffect.DepthOcclusionMode;
import com.supermap.ar.areffect.EffectView;
import com.supermap.data.Environment;
import com.supermap.hiar.ARFrame;
import com.supermap.hiar.ARPointCloud;

import java.io.IOException;
import java.io.InputStream;

public class TestActivity extends AppCompatActivity {

    AREffectView arView;
    private TextView messageTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.hideBottomUIMenu(this);
        Utils.hideTopUI(this);

        //设置许可
        Environment.setLicensePath(PathConfig.LICENSE_PATH);
        Environment.initialization(/*上下文*/this,/*是否是64位*/true);
        setContentView(R.layout.activity_test);
        messageTxt = findViewById(R.id.message_txt);

        arView = findViewById(R.id.ar_view);
        arView.setDepthOcclusionMode(DepthOcclusionMode.DEPTH_OCCLUSION_ENABLED);
        arView.addOnUpdateListener(new EffectView.OnUpdateListener() {
            @Override
            public void onUpdate() {
                ARFrame arFrame = arView.getArSceneView().getArFrame();
                System.out.println(arFrame.toString());
                ARPointCloud arPointCloud = arFrame.acquirePointCloud();
//                float[] array = arPointCloud.getPoints().array();
                StringBuffer buffer = new StringBuffer();
//                buffer.append("output:\n" + array);
                try {
                    Image image = arFrame.acquireDepthImage();
                    if (image != null){
                        buffer.append(image.toString());
                    }
                } catch (NotYetAvailableException e) {
                    buffer.append(e.toString());
                }
                messageTxt.setText(buffer.toString());
            }
        });

//        AREffectElement element = new AREffectElement(this).setParentNode(arView);
//        PitObjRenderer pitObjRenderer = new PitObjRenderer(this, element.getAnchorNode());
//        Bitmap bitmap1 = readAssets(PathConfig.VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B1);
//        Bitmap bitmap2 = readAssets(PathConfig.VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B1);
//        Bitmap bitmap3 = readAssets(PathConfig.VIEWPORT_FUNC_PIT_OBJ_TEXTURE_B1);
//        pitObjRenderer.setTextureBitmap(bitmap1,bitmap2,bitmap3);

    }
    public Bitmap readAssets(String fileName){
        //纹理
        Bitmap bitmap=null;
        try {
            AssetManager assets = getAssets();
            InputStream is = assets.open(fileName);
            bitmap= BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
        }
        return bitmap;
    }
}